package com.alibaba.dubbo.demo.consumer;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.demo.UserService;

import java.util.Scanner;

public class ManualConsumer {
    public static void main(String[] args) {
        /**
         * dubbo 服务消费者api形式进行服务消费
         */
        //声明应用 dubbo生态质检服务调用是基于应用的
        ApplicationConfig application = new ApplicationConfig("dubbo-refrence");
        //涉及注册中心
        RegistryConfig registryCenter = new RegistryConfig();
        registryCenter.setAddress("zookeeper://localhost:2181");

        //消费者消费
        //设置消费者全局配置
        ConsumerConfig consumerConfig = new ConsumerConfig();
        //设置默认的超时时间
        consumerConfig.setTimeout(1000*5);
        ReferenceConfig<UserService> userConfigReference = new ReferenceConfig<>();
        userConfigReference.setApplication(application);
//        List<RegistryConfig> registryConfigs = new ArrayList<>();
//        registryConfigs.add(registryCenter);
        userConfigReference.setRegistry(registryCenter);
        userConfigReference.setInterface(UserService.class);
        userConfigReference.setLoadbalance("roundrobin");
        //设置methodConfig 方法级别的dubbo参数包配置 比如方法名必填、重试次数、超时时间、负载均衡策略
        MethodConfig methodConfig = new MethodConfig();
        //方法名必填
//        methodConfig.setName("queryUserInfo");
        //超时时间
        methodConfig.setTimeout(1000 * 5);
        //重试次数
        methodConfig.setRetries(3);
        //获取服务（并非真实的对象而是代理对象）
        UserService userService = userConfigReference.get();

        Scanner scanner = new Scanner(System.in);
        String input = "";
        while (!"quit".equalsIgnoreCase(input)){
            input = scanner.next();
            System.out.println(userService.echo(input));
        }
    }
}
